package edu.hawaii.jmotif.sequitur.logic;

import java.util.ArrayList;
import edu.hawaii.jmotif.sequitur.logic.SAXPosition;

public class Periodicity {

	double avgPeriod;
	double SDofPeriods;
	double avgMotifLength;

	public Periodicity(ArrayList<SAXPosition> arrPosParam) {
		if (arrPosParam.size() > 0) {
			ArrayList<SAXPosition> arrPos = arrPosParam;

			double sumOfIntervals = 0;
			double sqDiffs = 0;
			double sumOfMotifLengths = 0;
			int sizeOfarrPos = arrPos.size();

			sumOfMotifLengths += arrPos.get(0).endPos - arrPos.get(0).startPos;

			for (int i = 1; i < sizeOfarrPos; i++) {

				double currentInterval = arrPos.get(i).startPos
						- arrPos.get(i - 1).startPos;

				sumOfIntervals += currentInterval;
				sumOfMotifLengths += arrPos.get(i).endPos
						- arrPos.get(i).startPos;

			}

			avgPeriod = sumOfIntervals / (sizeOfarrPos - 1);

			for (int i = 1; i < sizeOfarrPos; i++) {

				double currentInterval = arrPos.get(i).startPos
						- arrPos.get(i - 1).startPos;

				sqDiffs += Math.pow((currentInterval - avgPeriod), 2);

			}

			avgMotifLength = sumOfMotifLengths / sizeOfarrPos;
			SDofPeriods = Math.sqrt(sqDiffs / (sizeOfarrPos - 1));
		}
	}

	public double getAvgPeriod() {
		return avgPeriod;
	}

	public void setAvgPeriod(double avgPeriod) {
		this.avgPeriod = avgPeriod;
	}

	public double getSDofPeriods() {
		return SDofPeriods;
	}

	public void setSDofPeriods(double sDofPeriods) {
		SDofPeriods = sDofPeriods;
	}

	public double getAvgMotifLength() {
		return avgMotifLength;
	}

	public void setAvgMotifLength(double avgMotifLength) {
		this.avgMotifLength = avgMotifLength;
	}

	public static void main(String[] args) {

		ArrayList<SAXPosition> arrPosSample = new ArrayList<SAXPosition>();
		arrPosSample.add(new SAXPosition(2, 4));
		arrPosSample.add(new SAXPosition(5, 8));
		arrPosSample.add(new SAXPosition(9, 10));
		arrPosSample.add(new SAXPosition(12, 45));

		Periodicity testPeriodicityAnalysis = new Periodicity(arrPosSample);

		System.out.print(testPeriodicityAnalysis.getAvgMotifLength() + " "
				+ testPeriodicityAnalysis.getAvgPeriod() + " "
				+ testPeriodicityAnalysis.getSDofPeriods());

	}

}